package cn.maszy.service.impl.projectmanagement;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.maszy.common.Pager;
import cn.maszy.common.SessionFactory;
import cn.maszy.dao.projectmanagement.B_T_Ufp_EngineerCategoryMapper;
import cn.maszy.dao.projectmanagement.B_T_Ufp_EngineerMaterialMapper;
import cn.maszy.dao.projectmanagement.B_T_Ufp_EngineerPositionMapper;
import cn.maszy.dao.projectmanagement.B_T_Ufp_EngineeringMapper;
import cn.maszy.model.projectmanagement.B_T_Ufp_EngineerCategoryExample;
import cn.maszy.model.projectmanagement.B_T_Ufp_EngineerMaterialExample;
import cn.maszy.model.projectmanagement.B_T_Ufp_EngineerPosition;
import cn.maszy.model.projectmanagement.B_T_Ufp_EngineerPositionExample;
import cn.maszy.model.projectmanagement.B_T_Ufp_Engineering;
import cn.maszy.model.projectmanagement.B_T_Ufp_EngineeringExample;
import cn.maszy.model.projectmanagement.B_T_Ufp_EngineeringExample.Criteria;
import cn.maszy.service.projectmanagement.B_T_Ufp_EngineeringService;

@Service
public class B_T_Ufp_EngineeringServiceImpl implements
		B_T_Ufp_EngineeringService {
	@Autowired
	private B_T_Ufp_EngineeringMapper b_T_Ufp_EngineeringMapper;
	@Autowired
	private B_T_Ufp_EngineerPositionMapper b_T_Ufp_EngineerPositionMapper;
	@Autowired
	private B_T_Ufp_EngineerMaterialMapper b_T_Ufp_EngineerMaterialMapper;
	@Autowired
	private B_T_Ufp_EngineerCategoryMapper b_T_Ufp_EngineerCategoryMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.maszy.service.investment.B_T_Ufp_AdminService#update(cn.maszy.model
	 * .investment.B_T_Ufp_Engineering,
	 * cn.maszy.model.investment.B_T_Ufp_AdminExample)
	 */
	public int update(B_T_Ufp_Engineering b_T_Ufp_Engineering) {
		if (b_T_Ufp_Engineering.getSpare() != null
				&& !"".equals(b_T_Ufp_Engineering.getSpare())) {
			String ss = b_T_Ufp_Engineering.getSpare().replaceAll(",", "");
			b_T_Ufp_Engineering.setSpare(ss);
		}
		if (b_T_Ufp_Engineering.getStatus() != null
				&& !"".equals(b_T_Ufp_Engineering.getStatus())) {
			String sss = b_T_Ufp_Engineering.getStatus().replaceAll(",", "");
			b_T_Ufp_Engineering.setStatus(sss);
		}
		// TODO Auto-generated method stub
		if (b_T_Ufp_Engineering.getUuid() == null
				|| "".equals(b_T_Ufp_Engineering.getUuid())) {
			b_T_Ufp_Engineering.setUuid(UUID.randomUUID().toString());
			b_T_Ufp_Engineering.setIsDelete(0);
			b_T_Ufp_Engineering.setCreateTime(new Date());
			b_T_Ufp_Engineering.setCreateUser(SessionFactory
					.getSessionUserRealNam());
			return b_T_Ufp_EngineeringMapper.insert(b_T_Ufp_Engineering);
		} else {
			// 修改字表信息。
			B_T_Ufp_EngineerPosition record = new B_T_Ufp_EngineerPosition();
			record.setEngineeringName(b_T_Ufp_Engineering.getEngineeringName());
			record.setEngineeringType(b_T_Ufp_Engineering.getEngineeringType());
			record.setUpdateTime(new Date());
			record.setUpdateUser(SessionFactory.getSessionUserRealNam());
			B_T_Ufp_EngineerPositionExample example = new B_T_Ufp_EngineerPositionExample();
			example.createCriteria().andEngineeringUuidEqualTo(
					b_T_Ufp_Engineering.getUuid());
			b_T_Ufp_EngineerPositionMapper.updateByExampleSelective(record,
					example);

			b_T_Ufp_Engineering.setUpdateTime(new Date());
			b_T_Ufp_Engineering.setUpdateUser(SessionFactory
					.getSessionUserRealNam());
			return b_T_Ufp_EngineeringMapper
					.updateByPrimaryKeySelective(b_T_Ufp_Engineering);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.maszy.service.investment.B_T_Ufp_AdminService#selectOne(java.lang.
	 * String)
	 */
	public B_T_Ufp_Engineering selectOne(String uuid) {
		// TODO Auto-generated method stub
		B_T_Ufp_Engineering record = b_T_Ufp_EngineeringMapper
				.selectByPrimaryKey(uuid);
		return record;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.maszy.service.investment.B_T_Ufp_AdminService#delete(java.lang.String)
	 */
	public int delete(String checkIds) {
		// TODO Auto-generated method stub
		for (String id : checkIds.split(", ")) {
			// b_T_Ufp_EngineeringMapper.deleteByPrimaryKey(id);
			// 逻辑删除
			if (!"on".equals(id)) {
				B_T_Ufp_Engineering b_T_Ufp_Engineering = new B_T_Ufp_Engineering();
				// B_T_Ufp_EngineeringExample example =new
				// B_T_Ufp_EngineeringExample();
				// example.or().andUuidEqualTo(id);
				b_T_Ufp_Engineering = b_T_Ufp_EngineeringMapper
						.selectByPrimaryKey(id);
				b_T_Ufp_Engineering.setIsDelete(1);
				b_T_Ufp_EngineeringMapper
						.updateByPrimaryKeySelective(b_T_Ufp_Engineering);
			}
			// 根据需要删除的工程uuid查出项目部位的list对象；
			B_T_Ufp_EngineerPositionExample example2 = new B_T_Ufp_EngineerPositionExample();
			example2.createCriteria().andEngineeringUuidEqualTo(id);
			List<B_T_Ufp_EngineerPosition> listrecord = b_T_Ufp_EngineerPositionMapper
					.selectByExample(example2);

			// 联动删除项目部位表
			B_T_Ufp_EngineerPositionExample example = new B_T_Ufp_EngineerPositionExample();
			example.createCriteria().andEngineeringUuidEqualTo(id);
			b_T_Ufp_EngineerPositionMapper.deleteByExample(example);
			// 删除项目详细内容
			if (listrecord.size() > 0) {
				for (int i = 0; i < listrecord.size(); i++) {
					B_T_Ufp_EngineerPosition record = listrecord.get(i);
					B_T_Ufp_EngineerCategoryExample example3 = new B_T_Ufp_EngineerCategoryExample();
					example3.createCriteria().andPositionUuidEqualTo(
							record.getUuid());
					b_T_Ufp_EngineerCategoryMapper.deleteByExample(example3);
				}
			}

			// 删除材料表
			B_T_Ufp_EngineerMaterialExample example1 = new B_T_Ufp_EngineerMaterialExample();
			example1.createCriteria().andEngineeringUuidEqualTo(id);
			b_T_Ufp_EngineerMaterialMapper.deleteByExample(example1);
		}
		return 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.maszy.service.investment.B_T_Ufp_AdminService#count(cn.maszy.model
	 * .investment.B_T_Ufp_AdminExample)
	 */
	public int count(B_T_Ufp_EngineeringExample example,
			B_T_Ufp_EngineeringMapper b_T_Ufp_EngineeringMapper) {
		// TODO Auto-generated method stub
		return b_T_Ufp_EngineeringMapper.countByExample(example);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.maszy.service.investment.B_T_Ufp_AdminService#selectList(cn.maszy.
	 * model.investment.B_T_Ufp_Engineering)
	 */
	public Pager<B_T_Ufp_Engineering> selectList(
			B_T_Ufp_Engineering b_T_Ufp_Engineering,
			Pager<B_T_Ufp_Engineering> pager, String starttime, String endtime) {
		// TODO Auto-generated method stub
		// 添加查询条件
		B_T_Ufp_EngineeringExample example = new B_T_Ufp_EngineeringExample();
		Criteria criteria = example.createCriteria();
		if (!"".equals(starttime) && starttime != null) {
			criteria.andBudgetTimeGreaterThanOrEqualTo(starttime);
		}
		if (!"".equals(endtime) && endtime != null) {
			criteria.andBudgetTimeLessThanOrEqualTo(endtime);
		}

		if (b_T_Ufp_Engineering != null) {
			if (b_T_Ufp_Engineering.getEngineeringName() != null
					&& !"".equals(b_T_Ufp_Engineering.getEngineeringName())) {
				criteria.andEngineeringNameLike("%"
						+ b_T_Ufp_Engineering.getEngineeringName() + "%");
			}
			if (b_T_Ufp_Engineering.getDesignerName() != null
					&& !"".equals(b_T_Ufp_Engineering.getDesignerName())) {
				criteria.andDesignerNameLike("%"
						+ b_T_Ufp_Engineering.getDesignerName() + "%");
			}
			//如果当前用户不是管理员身份，
			if (b_T_Ufp_Engineering.getCreateUser() != null
					&& !"".equals(b_T_Ufp_Engineering.getCreateUser())) {
				criteria.andCreateUserEqualTo(b_T_Ufp_Engineering
						.getCreateUser());
				//如果当页面有查询条件的时候不查共享的数据
				if (b_T_Ufp_Engineering.getEngineeringName() != null
						&& !"".equals(b_T_Ufp_Engineering.getEngineeringName())
						|| b_T_Ufp_Engineering.getDesignerName() != null
						&& !"".equals(b_T_Ufp_Engineering.getDesignerName())
						|| !"".equals(starttime) && starttime != null
						|| !"".equals(endtime) && endtime != null) {
				} else {
					example.or().andIsShareEqualTo(1).andIsDeleteEqualTo(0);
				}
			}
		}
		// 查询isdelete为0的数据；
		criteria.andIsDeleteEqualTo(0);
		int totalCount = new B_T_Ufp_EngineeringServiceImpl().count(example,
				b_T_Ufp_EngineeringMapper);
		pager.setTotalRecords(totalCount);
		example.setLimitStart(pager.getStartRecord());
		example.setLimitEnd(pager.getPageSize());
		example.setOrderByClause(pager.getOrderField() + " "
				+ pager.getOrderDirection());

		// 取得总的记录条数
		List<B_T_Ufp_Engineering> list = b_T_Ufp_EngineeringMapper
				.selectByExample(example);
//		if(list !=null&&list.size()>0)
//		{
//			for(int i=0;i<list.size();i++)
//			{
//				try{
//				String engineeringName = java.net.URLEncoder.encode(list.get(i).getEngineeringName(),"UTF-8");
//				list.get(i).setEngineeringName(engineeringName);
//				}
//				catch (Exception e) {
//					// TODO: handle exception
//				}
//			}
//		}
		pager.setPageRecords(list);
		return pager;
	}

}
